//app.js

const utils = require("utils/request.js")

App({
  
  onLaunch(){
    //检测登陆授权
    this.checkUserInfo();
  },

  globalData: {
    token:null,
    userInfo: null,
    wxinfo:null,
    scopeu:false
  },

  audioData:null,


  

  checkUserInfo:function()
  {
    var that = this;
    console.log("请求授权");
    wx.getSetting({
      success(res) {
        console.log(res);
        //未授权
        if (!res.authSetting['scope.userInfo']) {
          that.globalData.userInfo = null;
          that.globalData.scopeu = false;
          wx.removeStorageSync('userInfo')
          wx.hideTabBar();
        }
        //已授权
        else {
          that.globalData.scopeu = true;
          var pages = getCurrentPages();
          var currPage = pages[pages.length - 1];
          if(currPage){
            currPage.setData({
              flag:false
            });
            wx.showTabBar({

            })
          }
         

          wx.getUserInfo({
            success: function (res) {
              var userInfo = res.userInfo;
              //console.log(userInfo);
              that.globalData.wxinfo = userInfo;
              that.wxcheckSession();
            }
          })
        }
      }
    })
  },
  wxcheckSession: function () {
    var that = this;
    //检测用户授权
    //用户登陆
    wx.checkSession({
      success() {
        
        //获取缓存用户信息
        wx.getStorage({
          key: 'userInfo',
          success: function (res) {
            //console.log("checkSession：success");
            //console.log(res);
            if (res.data != null){
              that.globalData.userInfo = res.data;
            }
            else{
              wx.removeStorageSync('userInfo')
              that.login();
            }
          },
          fail:function(){
            //console.log("fail");
            that.globalData.userInfo = null;
            wx.removeStorageSync('userInfo')
            that.login();
          }
        });
        wx.getStorage({
          key: 'token',
          success: function (res) {
            that.globalData.token = res.token;
          },
        })
        //session_key 未过期，并且在本生命周期一直有效
      },
      fail() {
        that.login();
        // session_key 已经失效，需要重新执行登录流程

      }
    })
  },
  login:function(){
    var that = this;
    //console.log(that.globalData.wxinfo);
    wx.login({
      success(res) {
        if (res.code) {
          //console.log(res);
          var data = { code: res.code}
          data.WX_NickName = that.globalData.wxinfo.nickName;
          data.WX_AvatarUrl = that.globalData.wxinfo.avatarUrl;
          data.WX_Gender = that.globalData.wxinfo.gender;
          data.NickName = that.globalData.wxinfo.nickName;
          data.AvatarUrl = that.globalData.wxinfo.avatarUrl;
          data.Gender = that.globalData.wxinfo.gender;
          data.WX_Province = that.globalData.wxinfo.province;
          data.WX_City = that.globalData.wxinfo.city;
          data.WX_Country = that.globalData.wxinfo.country;
          console.log(data);
          utils.request("api/Customer/GetWXInfo", data).then(
          function (res) {
            console.log(res.data.status);
              if(res.data.status == 1){
                that.globalData.userInfo = res.data.data;
                wx.setStorage({
                  key: 'userInfo',
                  data: that.globalData.userInfo,
                });
                wx.setStorage({
                  key: 'token',
                  data: that.globalData.userInfo.ID,
                })
              }
            }
          );
        }
      }
    })
  },
  //更新本地缓存
  updateUserInfo(){
    var that = this;
    new Promise(function(resolve){
      var data = { "id": that.globalData.userInfo.ID }
      console.log(data);
      utils.request("api/Customer/GetCustomerSimple", data).then(
        function (res) {
          console.log(res.data.status);
          if (res.data.status == 1) {
            that.globalData.userInfo = res.data.data;
            wx.setStorage({
              key: 'userInfo',
              data: that.globalData.userInfo,
            });
            wx.setStorage({
              key: 'token',
              data: that.globalData.userInfo.ID,
            })
          }
        }
      );
    })

  }


})